<?php
/*! \brief
 * This is WFWidgetPoolManager class
 * This is the Class that manipulate WidgetPool event
 * \author William Su
 */
require_once dirname(__FILE__) . '/WidgetPoolManager.abstract.php';
class WFWidgetComponentPoolManager extends AWidgetPoolManager
{
    public function __construct($objFramework){
        $strUpdateType = 'widget_component';
        parent::__construct($objFramework, $strUpdateType);
    }
    /**
     *\brief Description
     * - Add /path/to/widget_news/widget/modNewWidget to /path/to/widgetPool/wpLogId/widget/.
     * - EX : Upgrade /path/to/widget_news/widget/modTodo which version is 2.0 to /path/to/widgetPool/wpLogId/widget/modTodo is 1.0
     * - Due to this is the WidgetComponentPoolManager Class, so no need to do anything on widget
     */
    protected function addOrUpgradeWidgetFromWidgetsNew(){
        // do nothing
    }
    /**
     *\brief Description
     * - Add /path/to/widget_news/widgetComponent/comNewWidgetComponent to /path/to/widgetPool/wpLogId/widgetComponent/.
     * - EX : Upgrade /path/to/widget_news/widgetComponent/comTodo which version is 2.0 to /path/to/widgetPool/wpLogId/widgetComponent/comTodo is 1.0
     * - Only widget_component of product_id=$mixProductId will move
     *\par Variable
     * - EX : $assocsWcomponentNameWithWidgetComponentFromWidgetsNewByProductId
     * array (
          'comSPSFRS' => 
          array (
            'wcomponent_name' => 'comSPSFRS',
            'wcomponent_version' => '2.0',
            'wcomponent_min_support_version' => '2.0',
          ),
          'comSPSWRS' => 
          array (
            'wcomponent_name' => 'comSPSWRS',
            'wcomponent_version' => '1.0',
            'wcomponent_min_support_version' => '2.0',
          ),
        )
     */
    protected function addOrUpgradeWidgetComponentFromWidgetsNew($mixProductId){
        if(!$mixProductId){
            $message = __CLASS__.'::'.__FUNCTION__.'()=>Parameter $mixProductId is empty!';
        }
        $assocsWcomponentNameWithWidgetComponentFromWidgetsNewByProductId = $this->getAssocsWcomponentNameWithWidgetComponentFromWidgetsNewByProductId($mixProductId);
        $arrDir = array_keys($assocsWcomponentNameWithWidgetComponentFromWidgetsNewByProductId);
        if(is_array($arrDir) && $arrDir){
            foreach($arrDir as $strWidgetComponentName){
                $strPathWidgetComponentWPLogId = $this->getPathWPLogId() . '/widgetComponent/' . $strWidgetComponentName;
                if(false === file_exists($strPathWidgetComponentWPLogId)){
                    $this->addWidgetComponentFromWidgetsNewByWidgetComponentName($strWidgetComponentName);
                }else{
                    $this->upgradeWidgetComponentFromWidgetsNewByWidgetComponentName($strWidgetComponentName);
                }
            }
        }
        
        // move /path/to/widgets_new/widgetComponent/widget_component_info.xml to /path/to/repository/wigetPool/wpLogId
        $this->updateWidgetComponentInfoXmlProductTypeStatusFromWidgetsNewToWpLogIdByProductId($mixProductId);
    }
    /**
     * @see AWidgetPoolManager::updateSuccessResultToWidgetsNewInfoXml()
     *\brief Description
     * - update success status of widgets_new_info.xml => widget_components_update->product_types->product_type[@id=$mixProductId]->update_result=1
     */
    protected function updateSuccessResultToWidgetsNewInfoXml($mixProductId){
        $intUpdateResult = WFWidgetPoolOpt::WP_UPDATE_RESULT_UPDATE_SUCCESS;
        $this->objWidgetsNewInfoXmlManager->updateWidgetComponentUpdateResultToWidgetsNewInfoXml($intUpdateResult, $mixProductId);
    }
    /**
     * @see AWidgetPoolManager::updateFailedResultToWidgetsNewInfoXml()
     *\brief Description
     * - update failed status of widgets_new_info.xml => widget_components_update->product_types->product_type[@id=$mixProductId]->update_result=2
     */
    protected function updateFailedResultToWidgetsNewInfoXml($mixProductId){
        $intUpdateResult = WFWidgetPoolOpt::WP_UPDATE_RESULT_UPDATE_FAILED;
        $this->objWidgetsNewInfoXmlManager->updateWidgetComponentUpdateResultToWidgetsNewInfoXml($intUpdateResult, $mixProductId);
    }
    /**
     *\par Variable
     * - EX : $ssocsUpdateStatusForConfigureWidget
     * array (
          'configure_widget' => 
          array (
            'current_version' => '',
            'new_version' => '1.0',
            'update_status' => '',
          ),
          'widget_component' => 
          array (
            0 => 
            array (
              'product_id' => '15.4',
              'current_version' => '',
              'new_version' => '1.0',
              'update_status' => 'update',
            ),
            1 => 
            array (
              'product_id' => '1001.2',
              'current_version' => '',
              'new_version' => '1.0',
              'update_status' => 'ok',
            ),
          ),
        )
     */
    public function getAssocsUpdateStatusForConfigureWidgetAndWidgetComponent($isNewWcuUpdate){
        
        $assocProductIdWithWidgetVersionFromWpCurrent = $this->objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWpCurrent();
        $objLangFileManager = WF::getLangFileFactory()->getLangFileManager();		
      	$strResult = $objLangFileManager->getLangFromMapping($_COOKIE['LANG']);  
        $strConfigureWidgetVersionFromWpCurrent = $this->objWidgetInfoXmlManager->getConfigureWidgetVersionFromWpCurrent();
        //var_export($arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml);exit();
        
        //Step 1 : check scm update version check result
        if( $isNewWcuUpdate === true ){
        	
        	//Step 2 : check update status from /path/to/repository/widgets_new_info.xml
        	$arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml = $this->objWidgetsNewInfoXmlManager->getArrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml();
        	 
        	//If /path/to/repository/widgets_new is existed
        	if(is_array($arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml) && $arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml){
	            // configure_widget
	            $assocProductIdWithWidgetVersionFromWidgetsNew = $this->objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWidgetsNew();
	            $strConfigureWidgetVersionFromWidgetsNew = $this->objWidgetInfoXmlManager->getConfigureWidgetVersionFromWidgetsNew();
	            
	            $ret = array();
	            $intConfigureWidgetUpdateStatus = $this->getConfigureWidgetUpdateStatusByUpdateStatusAndUpdateResult();
	            $update_status = $this->getUpdateStatusForUIByUpdateStatus($intConfigureWidgetUpdateStatus);
	            
	            $helper_url = ''; //default         
	            $strWebRootPathConfigureWidgetHelper = 'repository/widgets_new/helper/configure_widget/'.$strResult.'/helper.htm';
	            $strPathConfigureWidgetHelper = $this->objFramework->getDocumentRoot().'/'.$strWebRootPathConfigureWidgetHelper;
	            if(file_exists($strPathConfigureWidgetHelper)){
	                $helper_url = $strWebRootPathConfigureWidgetHelper;
	            }
	            $ret['configure_widget'] = array(
	                'current_version' => $strConfigureWidgetVersionFromWpCurrent,
	                'new_version' => $strConfigureWidgetVersionFromWidgetsNew,
	                'update_status' => $update_status,
	                'helper_url' => $helper_url
	            );
	            
	            // widget_component
	            foreach($arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml as $assocWidgetComponentUpdateStatusFromWidgetsNewInfoXml){
	                $intUpdateStatus = $this->getUpdateStatusByCondition($assocWidgetComponentUpdateStatusFromWidgetsNewInfoXml['update_status'],$assocWidgetComponentUpdateStatusFromWidgetsNewInfoXml['update_result']);
	                $update_status = $this->getUpdateStatusForUIByUpdateStatus($intUpdateStatus);
	                $mixProductId = $assocWidgetComponentUpdateStatusFromWidgetsNewInfoXml['@attributes']['id'];
	                $strWidgetComponentVersionFromWpCurrent = isset($assocProductIdWithWidgetVersionFromWpCurrent[$mixProductId]['widget_component_version']) ? $assocProductIdWithWidgetVersionFromWpCurrent[$mixProductId]['widget_component_version'] : '';
	                $strWidgetComponentVersionFromWidgetsNew = isset($assocProductIdWithWidgetVersionFromWidgetsNew[$mixProductId]['widget_component_version']) ? $assocProductIdWithWidgetVersionFromWidgetsNew[$mixProductId]['widget_component_version'] : '';
	    
	                $helper_url = ''; //default
	
	                $strWebRootPathWidgetComponentHelper = 'repository/widgets_new/helper/widget_component/'.$mixProductId.'/'.$strResult.'/helper.htm';
	                $strPathWidgetComponentHelper = $this->objFramework->getDocumentRoot().'/'.$strWebRootPathWidgetComponentHelper;
	                if(file_exists($strPathWidgetComponentHelper)){
	                    $helper_url = $strWebRootPathWidgetComponentHelper;
	                }
	
	                $tip_content = ''; //default                                   
	                $strWidgetComponentHelperPath = 'helper/widget_component/'.$mixProductId.'/'.$strResult;
	                $strWidgetComponentTipPath = $this->getWPHomePath().'/'.$strWidgetComponentHelperPath.'/tip.htm';
	                if(file_exists($strWidgetComponentTipPath)){
	                    $tip_content = file_get_contents($strWidgetComponentTipPath);
	                }
	                $ret['widget_component'][] = array(
	                    'product_id' => $mixProductId,
	                    'current_version' => $strWidgetComponentVersionFromWpCurrent,
	                    'new_version' => $strWidgetComponentVersionFromWidgetsNew,
	                    'update_status' => $update_status,
	                    'helper_url' => $helper_url,
	                    'tip_content' => $tip_content
	                );
	            }
	            
	            return $ret;
	        }
        }	
        // if /path/to/repository is not existed
        // configure_widget
        $strConfigureWidgetVersionFromWidgetsNew = '';
        $update_status = $this->objFramework->getWidgetPoolFactory()->getWidgetPoolOpt()->getUpdateStatusForUI('0'); //no need to update
        $helper_url = '';
        $ret['configure_widget'] = array(
            'current_version' => $strConfigureWidgetVersionFromWpCurrent,
            'new_version' => $strConfigureWidgetVersionFromWidgetsNew,
            'update_status' => $update_status,
            'helper_url' => $helper_url
        );
            
        // widget_component
        //Step 1 : check update status from /path/to/repository/widgets_new_info.xml
        $assocProductIdWithWidgetVersionFromWpCurrent = $this->objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWpCurrent();
        if(is_array($assocProductIdWithWidgetVersionFromWpCurrent) && $assocProductIdWithWidgetVersionFromWpCurrent){
            foreach($assocProductIdWithWidgetVersionFromWpCurrent as $strProductId => $assocWidgetVersionFromWpCurrent){
                $mixProductId = $assocWidgetVersionFromWpCurrent['@attributes']['id'];
                $strWidgetComponentVersionFromWpCurrent = $assocWidgetVersionFromWpCurrent['widget_component_version'];
                $strWidgetComponentVersionFromWidgetsNew = '';
                $update_status = $this->objFramework->getWidgetPoolFactory()->getWidgetPoolOpt()->getUpdateStatusForUI('0'); //no need to update
                $helper_url = '';
                $tip_content = ''; //default                                    
				$strWidgetComponentHelperPath = 'helper/widget_component/'.$mixProductId.'/'.$strResult;
				$strWidgetComponentTipPath = $this->getWPHomePath().'/'.$strWidgetComponentHelperPath.'/tip.htm';
                if(file_exists($strWidgetComponentTipPath)){
                    $tip_content = file_get_contents($strWidgetComponentTipPath);
                }
                $ret['widget_component'][] = array(
                    'product_id' => $mixProductId,
                    'current_version' => $strWidgetComponentVersionFromWpCurrent,
                    'new_version' => $strWidgetComponentVersionFromWidgetsNew,
                    'update_status' => $update_status,
                    'helper_url' => $helper_url,
                    'tip_content' => $tip_content
                );
            }
        }
        
        return $ret;
    }
    /**
     *\par Variable
     * - EX : $strUpdateStatusForUIByUpdateStatus = update/ok/enable
     */
    protected function getUpdateStatusForUIByUpdateStatus($intUpdateStatus){
        $ret = $this->objFramework->getWidgetPoolFactory()->getWidgetPoolOpt()->getUpdateStatusForUI($intUpdateStatus);
        return $ret;
    }
    /**
     *\brief Description
     * - // move /path/to/widgets_new/widgetComponent/widget_component_info.xml status to /path/to/repository/wigetPool/wpLogId
     * - If product_id=1001.2 in /path/to/repository/widgets_new/widgetComponent/widget_component_info.xml is new => add below tag to widget_component_info.xml
     * <product_type id="1001.2" name="SPS">
            <widget_component_version>1.0</widget_component_version>
        </product_type>
     * - If product_id=1001.2 in /path/to/repository/widgets_new/widgetComponent/widget_component_info.xml need to upgrade to 2.0 => update below tag to widget_component_info.xml
     * <product_type id="1001.2" name="SPS">
            <widget_component_version>2.0</widget_component_version>
        </product_type>
     */
    protected function updateWidgetComponentInfoXmlProductTypeStatusFromWidgetsNewToWpLogIdByProductId($mixProductId){
        $arrProductTypeFromWidgetsNew = $this->objWidgetComponentInfoXmlManager->getArrProductTypeFromWidgetsNewByProductId($mixProductId);
        $isUpdate = false;
        
        $assocProductIdWithWidgetVersionFromWpCurrent = $this->objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWpCurrent();
        if(is_array($assocProductIdWithWidgetVersionFromWpCurrent) && $assocProductIdWithWidgetVersionFromWpCurrent){
            foreach($assocProductIdWithWidgetVersionFromWpCurrent as $mixProductIdOfAssoc => $arrProductIdWithWidgetVersionFromWpCurrent){
                if($mixProductIdOfAssoc == $mixProductId){
                    $intWpLogId = $this->getWPLogId();
                    $strWidgetComponentVersionWidgetsNew = $arrProductTypeFromWidgetsNew['widget_component_version'];
                    $this->objWidgetComponentInfoXmlManager->updateWpLogIdWidgetComponentVersionByProductId($intWpLogId, $mixProductId, $strWidgetComponentVersionWidgetsNew);
                    $isUpdate = true;
                    break;
                }
            }
        }
        if(false === $isUpdate){ //Add
            $intWpLogId = $this->getWPLogId();
            $this->objWidgetComponentInfoXmlManager->addWpLogIdProductTypeElementByArrProductType($intWpLogId, $arrProductTypeFromWidgetsNew);
        }
    }
}
